package org.jix.gbook.auth.rpc;

import jakarta.annotation.Resource;
import org.jix.framework.common.response.Response;
import org.jix.gbook.user.api.UserFeignApi;
import org.jix.gbook.user.dto.req.FindUserByPhoneReqDTO;
import org.jix.gbook.user.dto.req.RegisterUserReqDTO;
import org.jix.gbook.user.dto.req.UpdateUserPasswordReqDTO;
import org.jix.gbook.user.dto.resp.FindUserByPhoneRspDTO;
import org.springframework.stereotype.Component;

/**
 * @author stark
 * @date 2025/7/12 17:24
 * @description
 **/
@Component
public class UserRPCService {

    @Resource
    private UserFeignApi userFeignApi;

    /**
     * 用户注册
     * @param phone
     * @return
     */
    public Long registerUser(String phone){
        RegisterUserReqDTO registerUserReqDTO = new RegisterUserReqDTO();
        registerUserReqDTO.setPhone(phone);

        Response<Long> response = userFeignApi.registerUser(registerUserReqDTO);

        if (!response.isSuccess()){
            return null;
        }
        return response.getData();
    }

    /**
     * 根据手机号查询用户信息
     * @param phone
     * @return
     */
    public FindUserByPhoneRspDTO findUserByPhone(String phone){
        FindUserByPhoneReqDTO findUserByPhoneReqDTO = new FindUserByPhoneReqDTO();
        findUserByPhoneReqDTO.setPhone(phone);

        Response<FindUserByPhoneRspDTO> response = userFeignApi.findByPhone(findUserByPhoneReqDTO);

        if (!response.isSuccess()){
            return null;
        }
        return response.getData();
    }

    /**
     * 更新密码
     * @param encodePassword
     */
    public void updatePassword(String encodePassword){
        UpdateUserPasswordReqDTO updateUserPasswordReqDTO = new UpdateUserPasswordReqDTO();
        updateUserPasswordReqDTO.setEncodePassword(encodePassword);

        userFeignApi.updatePassword(updateUserPasswordReqDTO);
    }
}
